package product;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SearchPro extends JFrame {
	private JScrollPane scrollPane;
	private JTable table;
	DefaultTableModel model;
	Vector<String> col = new Vector<String>();
	Connection con;
	String dept,dept1;
	BarChart bar;
	ProductMain2 pm;
	private JPanel panel;
	private JTextField textField;
	private JButton btnNewButton;
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					SearchPro frame = new SearchPro();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 * @throws Exception 
	 */
	public SearchPro(ProductMain2 pm) throws Exception {
		this.pm=pm;
		setBounds(100, 100, 450, 300);
		col.add("제품코드");
		col.add("제품명");
		
		model=new DefaultTableModel(col,0);
		getContentPane().setLayout(null);
		
		getContentPane().add(getScrollPane());
		getContentPane().add(getPanel());
		conn();
	}
	void DbCon()throws Exception{
		 Class.forName("oracle.jdbc.driver.OracleDriver");
		 System.out.println("드라이버 로딩 성공...");
	      String url="jdbc:oracle:thin:@192.168.10.4:1521:XE";
	        
	      String user="hr";
	      String pwd="hr";
	        
	     con=DriverManager.getConnection(url,user,pwd);
	      System.out.println("DB 연결 성공!");
	}
	public void conn()throws Exception{

		DbCon();

	      Statement st=con.createStatement();

	      String sql="select c.pro_code,c.pro_nm from stock s,code c where c.pro_code=s.s_code";
	      ResultSet rs=st.executeQuery(sql);

	      while(rs.next()){
	    	  Vector<String> v=new Vector<String>();
	    	   dept=rs.getString(1);
	          dept1=rs.getString(2);
	        
	          v.add(dept);
	          v.add(dept1);

		
	          model.addRow(v);
	      }

	      rs.close();
	      st.close();
	      con.close();
	}
	public void conn2()throws Exception{

		DbCon();
		model.setRowCount(0);
	      Statement st=con.createStatement();
	      String str=textField.getText();
	      String sql="select c.pro_code,c.pro_nm from stock s,code c where c.pro_code=s.s_code and (pro_code"
	      		+ " like '%"+str+"%' or pro_nm like '%"+str+"%')";
	      ResultSet rs=st.executeQuery(sql);

	      while(rs.next()){
	    	  Vector<String> v=new Vector<String>();
	    	   dept=rs.getString(1);
	          dept1=rs.getString(2);
	        
	          v.add(dept);
	          v.add(dept1);

		
	          model.addRow(v);
	      }

	      rs.close();
	      st.close();
	      con.close();
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(0, 31, 150, 231);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					
					int row = table.getSelectedRow();
					String s = (String)table.getValueAt(row, 0);	
					System.out.println(s);
					if(pm.line!=null){
						pm.line.setData(s);
					}else if(pm.bc != null){
						pm.bc.setData(s);
					}
				}
			});
		}
		return table;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBounds(0, 0, 150, 34);
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getTextField(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setColumns(1);
		}
		return textField;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						conn2();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
			});
		}
		return btnNewButton;
	}
}
